﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmKetQuaHocTap_SinhVien : FormBase
    {
        public frmKetQuaHocTap_SinhVien()
        {
            InitializeComponent();
        }

        public int NamThucTapID { get; set; }
        public int SinhVienID { get; set; }

        private void frmKetQuaHocTap_SinhVien_Load(object sender, EventArgs e)
        {
            try
            {
                LoadSinhVien();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckInput())
                {
                    using (NestedUnitOfWork nuow = Session.DefaultSession.BeginNestedUnitOfWork())
                    {
                        var obj = nuow.Query<SinhVienTrongGiangVien>().FirstOrDefault(c => c.SinhVien.Oid == SinhVienID && c.GiangVienTrongNam.NamThucTap.Oid == NamThucTapID);
                        if (obj != null)
                        {
                            obj.Diem = Convert.ToDouble(txtDiem.Text);
                            obj.NhanXet = txtNhanXet.Text;
                            nuow.CommitChanges();
                        }
                    }
                    CommonMessage("Cập nhật thành công");
                    this.DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void LoadSinhVien()
        {
            var obj = Session.DefaultSession.Query<SinhVienTrongGiangVien>().Where(c => c.SinhVien.Oid == SinhVienID && c.GiangVienTrongNam.NamThucTap.Oid == NamThucTapID).Select(c => new
            {
                c.Oid,
                c.SinhVien.MaSinhVien,
                c.SinhVien.HoSinhVien,
                c.SinhVien.TenSinhVien,
                c.Diem,
                c.NhanXet
            }).FirstOrDefault();
            if (obj != null)
            {
                txtDiem.Text = obj.Diem.ToString("#0.##");
                txtNhanXet.Text = obj.NhanXet;
            }
        }

        private bool CheckInput()
        {
            bool flag = true;
            dxErrorProvider1.ClearErrors();
            if (txtDiem.Text == string.Empty)
            {
                flag = false;
                dxErrorProvider1.SetError(txtDiem, "Chưa nhập");
            }
            else
            {
                try
                {
                    double i = Convert.ToDouble(txtDiem.Text);
                }
                catch
                {
                    flag = false;
                    dxErrorProvider1.SetError(txtDiem, "Nhập sai");
                }
            }
            return flag;
        }
    }
}